Skip to content

Decode URI components with cgi/escape - #276

Open
thomasklemm wants to merge 1 commit into
ruby:masterfrom
thomasklemm:thomasklemm/decode-www-form-component-cgi
Open

thomasklemm wants to merge 1 commit into
ruby:masterfrom
thomasklemm:thomasklemm/decode-www-form-component-cgi

Conversation

@thomasklemm

@thomasklemm thomasklemm commented Nov 27, 2026 •

Copy link
Copy Markdown

URI.decode_www_form_component and URI.decode_uri_component copy the string with b and then run gsub with a regexp and a Hash, which costs a match, a substring and a Hash lookup for every + and %XX. Rack decodes query strings and cookies with decode_www_form_component (Rack::QueryParser and Rack::Utils.unescape in Rack 3.2.9), and Rails decodes query parameters and URL-encoded form fields with it (ActionDispatch::QueryParser, Rails 8.1.4.1).

The shared helper now yields the checked copy to CGI.unescape or CGI.unescapeURIComponent, which decode in one pass in C (in Java on JRuby). Results have the same class, bytes and encoding as before. Where cgi/escape has no native unescapeURIComponent (cgi before 0.3.3, TruffleRuby), both methods keep the gsub. uri now requires cgi/escape, which every supported Ruby ships.

benchmark-ips, Ruby 4.0.10 Before After
decode_www_form_component("hello") 2.84M i/s 3.43M i/s
decode_www_form_component("article%5Btitle%5D") 880.8k i/s 2.49M i/s
decode_www_form_component("Hello+world+from+Rack") 739.8k i/s 2.92M i/s
decode_www_form_component on a 248-byte cookie value 493.9k i/s 1.08M i/s
decode_uri_component("hello") 2.83M i/s 3.40M i/s
decode_uri_component("user%40example.com") 1.13M i/s 2.84M i/s

With YJIT, and with ZJIT on Ruby master, time per call drops by 23% to 80%. Each call allocates 2 or 3 objects instead of 4 to 8.

@thomasklemm
thomasklemm marked this pull request as draft November 27, 2026 21:10
Comment thread lib/uri/common.rb Resolved
URI.decode_www_form_component and URI.decode_uri_component copy the
String with `b` and then run `gsub` with a regexp and a Hash table,
which costs a match, a substring and a Hash lookup for every "+" and
"%XX". Rack and Rails decode query strings, URL-encoded form fields and
cookies through decode_www_form_component.

The shared _decode_uri_component keeps the lone-"%" check, the `b` copy
and the final force_encoding, and now takes the decoding step as a
block. Where cgi/escape implements unescapeURIComponent natively (in C
since cgi 0.3.3, in Java since 0.4.1), decode_www_form_component passes
CGI.unescape and decode_uri_component passes CGI.unescapeURIComponent,
which keeps "+"; both decode in one pass. Both get the BINARY copy, so
the result has the same class, bytes and encoding as before.

Elsewhere both methods keep the `gsub`: older cgi has no
unescapeURIComponent, and on TruffleRuby cgi/escape is written in Ruby,
where CGI.unescapeURIComponent keeps the String subclass that String#b
returns there.

URI.decode_www_form keeps its `gsub!`: it leaves a lone "%" as is, and
for the plain keys and values most forms have, CGI.unescape would
return a new String where `gsub!` changes nothing.
@thomasklemm
thomasklemm force-pushed the thomasklemm/decode-www-form-component-cgi branch from c43954f to e2d2e21 Compare November 28, 2026 00:15
@thomasklemm thomasklemm changed the title Decode www-form components with cgi/escape Decode URI components with cgi/escape Nov 28, 2026
@thomasklemm
thomasklemm marked this pull request as ready for review November 28, 2026 00:16

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant